﻿
Public Class grupo_data : Inherits Adaptador

    Event ErrorServer(ByVal Descripcion As String) 'evento de error que propaga el error de la clase adaptador (conexion a base de datos)
    Event GuardaCorrecto() 'evento propio de la clase, cuando guarda o realiza alguna operacion con exito.

    ''' <summary>
    ''' Devuelve todos los registros
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetData() As DataTable
        Conectar()
        Try
            Dim SQL As String
            SQL = "SELECT * FROM GRUPO ORDER BY GRUPO_NOMBRE"
            comando.CommandText = SQL
            comando.Connection = conn
            myReader = comando.ExecuteReader
            Dim dt As DataTable = Me.GetTable(myReader)
            Desconectar()
            Return dt
        Catch ex As Exception
            Desconectar()
            RaiseEvent ErrorServer(ex.Message)
        End Try
    End Function

    ''' <summary>
    ''' Buscar registros por COD/ID
    ''' </summary>
    ''' <param name="ID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function msoBuscar(ByVal ID As Integer) As DataTable
        Conectar()
        Try
            Dim SQL As String
            SQL = "SELECT * FROM GRUPO WHERE GRUPO_ID = " & ID
            comando.CommandText = SQL
            comando.Connection = conn
            myReader = comando.ExecuteReader
            Dim dt As DataTable = Me.GetTable(myReader)
            Desconectar()
            Return dt
        Catch ex As Exception
            Desconectar()
            RaiseEvent ErrorServer(ex.Message)
        End Try
    End Function

    Public Function msoBuscar(ByVal NOMBRE As String) As DataTable
        Conectar()
        Try
            Dim SQL As String
            SQL = "SELECT * FROM GRUPO WHERE  GRUPO_NOMBRE LIKE '%" & NOMBRE & "%'"
            comando.CommandText = SQL
            comando.Connection = conn
            myReader = comando.ExecuteReader
            Dim dt As DataTable = Me.GetTable(myReader)
            Desconectar()
            Return dt
        Catch ex As Exception
            Desconectar()
            RaiseEvent ErrorServer(ex.Message)
        End Try
    End Function

    ''' <summary>
    ''' guardar y actualizar
    ''' </summary>
    ''' <param name="ID"></param>
    ''' <param name="NOMBRE"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function minGuarda(ByVal ID As Integer, _
                                    ByVal NOMBRE As String)

        Try
            Dim SQL As String
            Dim dt As DataTable = msoBuscar(ID)
            Conectar()
            If dt.Rows.Count = 0 Then

                SQL = "INSERT INTO GRUPO(GRUPO_NOMBRE) " _
                                         & "values(" _
                                         & "'" & NOMBRE & "')"
            Else
                SQL = "UPDATE GRUPO SET " _
                                    & " GRUPO_NOMBRE = '" & NOMBRE & "' " _
                                    & " WHERE GRUPO_ID = " & ID & ""
            End If
            comando.CommandText = SQL
            comando.Connection = conn
            comando.ExecuteNonQuery()
            Desconectar()
            RaiseEvent GuardaCorrecto()
        Catch ex As Exception
            Desconectar()
            RaiseEvent ErrorServer(ex.Message)
        End Try
    End Function

    ''' <summary>
    ''' Eliminar registro
    ''' </summary>
    ''' <param name="ID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function mdnElimina(ByVal ID As Integer)

        Try
            Dim SQL As String
            Conectar()
            SQL = "DELETE FROM GRUPO WHERE GRUPO_ID = " & ID
            comando.CommandText = SQL
            comando.Connection = conn
            comando.ExecuteNonQuery()
            Desconectar()
            RaiseEvent GuardaCorrecto()
        Catch ex As Exception
            Desconectar()
            RaiseEvent ErrorServer(ex.Message)
        End Try
    End Function


    ''' <summary>
    ''' Devuelve error especifico de la conexion (clase adaptador)
    ''' </summary>
    ''' <param name="Descripcion"></param>
    ''' <remarks></remarks>
    Private Sub Adaptador_ErrorBaseServer(ByVal Descripcion As String) Handles Me.ErrorBaseServer
        RaiseEvent ErrorServer(Descripcion)
    End Sub
End Class
